﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;
using CIT.WMS.RM.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmRMGroupF : Form
    {

       
        public FrmRMGroupF()
        {
            InitializeComponent();
        }

        private void FrmRMGroupF_Load(object sender, EventArgs e)
        {
            ddlERPNo.Focus();
            BindERPNo();
        }

        public void BindERPNo() 
        {
             var ERPNoDt = Bll_RMIn.GetGroupERPNo();
             ddlERPNo.Text = "";
             ddlERPNo.Items.Clear();
             if (ERPNoDt != null && ERPNoDt.Rows.Count > 0)
             {
                 foreach (DataRow dr in ERPNoDt.Rows)
                 {
                     ddlERPNo.Items.Add(dr["RelatedBill"].ToString());
                 }
             }
        }

        private void ddlERPNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                bandPartNumber();
            }
        }

        public void bandPartNumber()
        {
            ddlPartNumber.Text = "";
            ddlPartNumber.Items.Clear();
            var PartDt = Bll_RMIn.GetGroupPartNumberByRela(ddlERPNo.Text);
            if (PartDt != null && PartDt.Rows.Count > 0)
            {
                foreach (DataRow dr in PartDt.Rows)
                {
                    ddlPartNumber.Items.Add(dr["PartNumber"].ToString());
                }
            }
        }

        private void ddlPartNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                bindListBox();
            }
        }

        public void bindListBox() 
        {
            listBox1.Items.Clear();
            var GroupDt = Bll_RMIn.GetGroupRowsByCondition(ddlERPNo.Text, ddlPartNumber.Text);
            if (GroupDt != null && GroupDt.Rows.Count > 0)
            {
                foreach (DataRow dr in GroupDt.Rows)
                {
                    var ShowText = dr["BillNo"].ToString() + "-" + dr["RowNo"].ToString() + "-" + dr["OrderNo"].ToString() + " " + dr["GroupSum"].ToString() + "/" + dr["PlanQty"].ToString() + dr["Unit"].ToString();
                    listBox1.Items.Add(ShowText);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

            DialogResult dr = MessageBox.Show("分配订单信息[" + listBox1.SelectedItem + "]，是否确认？", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

            if (dr == DialogResult.Cancel)
            {
                return;
            }

            var ListSeclet = listBox1.SelectedItem.ToString().Substring(0, listBox1.SelectedItem.ToString().IndexOf(" ")).Split('-');
          
            FrmRMGroupS fr = new FrmRMGroupS(ListSeclet[0],ListSeclet[1],ListSeclet[2]);
            fr.ShowDialog();

            bindListBox();
        }

        private void ddlERPNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            bandPartNumber();
        }

        private void ddlPartNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindListBox();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                var ListSeclet = listBox1.SelectedItem.ToString().Substring(0, listBox1.SelectedItem.ToString().IndexOf(" ")).Split('-');

                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                DialogResult dr = MessageBox.Show("请确认[单号为：" + ListSeclet[0] + "]的采购入库单已分配完成！", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                var NoGroupDt = Bll_RMIn.GetNoGroupRows(ListSeclet[0]);
                if (NoGroupDt != null && NoGroupDt.Rows.Count > 0)
                {
                    MessageBox.Show("该采购单还有未分组完成的行数据。");
                    return;
                }
                else
                {
                    //修改主表状态

                    bool isSuccess = Bll_RMIn.UpdateGroupStatus(ListSeclet[0]);
                    if (isSuccess)
                    {
                        MessageBox.Show("操作成功！");
                    }
                }
            }
            else 
            {
                MessageBox.Show("请选择分配信息！");
                return;
            }
        }

    }
}